﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HdipCricketClub.Models;
using PagedList;

namespace HdipCricketClub.Controllers
{ 
    public class MemberController : Controller
    {
        private CricketClubEntities db = new CricketClubEntities();

        MemberRepository memberRepository = new MemberRepository();

        //
        //  GET: Member/List

        public ViewResult List(string sortOrder, string searchString, int? page)
        {
            ViewBag.LastNameSortParam = String.IsNullOrEmpty(sortOrder);

            var members = memberRepository.FindAllMembers();
                          
            if (!String.IsNullOrEmpty(searchString))
            {
                members = members.Where(m => m.last_name.ToUpper().Contains(searchString.ToUpper()));
            }

            int pageSize = 10;
            int pageNumber = (page ?? 1);
            
            return View(members.ToPagedList(pageNumber, pageSize));
        }
                
        //
        // GET: /Member/

        public ViewResult Index(string sortOrder, string searchString, int? page)
        {
            ViewBag.LastNameSortParam = String.IsNullOrEmpty(sortOrder);

            var members = memberRepository.FindAllMembers();

            if (!String.IsNullOrEmpty(searchString))
            {
                members = members.Where(m => m.last_name.ToUpper().Contains(searchString.ToUpper()));
            }

            int pageSize = 10;
            int pageNumber = (page ?? 1);

            return View(members.ToPagedList(pageNumber, pageSize));
        }

        //
        // GET: /Member/Details/5

        public ViewResult Details(int id)
        {
            Member member = memberRepository.GetMember(id);
                      
            return View(member);
        }

        //
        // GET: /Member/Create

        public ActionResult Create()
        {
            ViewBag.club_id = new SelectList(db.Clubs, "club_id", "name");
            ViewBag.county = new SelectList(db.Counties, "county_id", "name");
            return View();
        } 

        //
        // POST: /Member/Create

        [HttpPost]
        public ActionResult Create(Member member)
        {
            if (ModelState.IsValid)
            {
                db.Members.AddObject(member);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.club_id = new SelectList(db.Clubs, "club_id", "name", member.club_id);
            ViewBag.county = new SelectList(db.Counties, "county_id", "name", member.county);
            return View(member);
        }
        
        //
        // GET: /Member/Edit/5
 
        public ActionResult Edit(int id)
        {
            Member member = memberRepository.GetMember(id);
            ViewBag.club_id = new SelectList(db.Clubs, "club_id", "name", member.club_id);
            ViewBag.county = new SelectList(db.Counties, "county_id", "name", member.county);
            return View(member);
        }

        //
        // POST: /Member/Edit/5

        [HttpPost]
        public ActionResult Edit(Member member)
        {
            if (ModelState.IsValid)
            {
                db.Members.Attach(member);
                db.ObjectStateManager.ChangeObjectState(member, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.club_id = new SelectList(db.Clubs, "club_id", "name", member.club_id);
            ViewBag.county = new SelectList(db.Counties, "county_id", "name", member.county);
            return View(member);
        }

        //
        // GET: /Member/Delete/5
 
        public ActionResult Delete(int id)
        {
            Member member = memberRepository.GetMember(id);
            return View(member);
        }

        //
        // POST: /Member/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            try
            {
                Member member = memberRepository.GetMember(id);
                db.Members.DeleteObject(member);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch (Exception)
            {
                Member member = memberRepository.GetMember(id);
                
                return RedirectToAction("UpdateError", new { id = member.member_id });
            }
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        //
        //  GET: Member/UpdateError/

        public ActionResult UpdateError(int id)
        {
            Member member = memberRepository.GetMember(id);

            return View(member);
        }

    }
}